package test.freshui.ffmpeg;

import android.util.Log;

public class PlayerControl extends Thread{
	private boolean isExit = false;
	private boolean needDecoding = false;
	private PlayerView mPlayerView = null;
	
	PlayerControl(PlayerView view){
		mPlayerView = view;
		Log.d("PlayerControl", "PlayerControl inikt");
		Native_initFfmpeg();
	}
	
	public boolean startPlay(String path){
		Native_setupFile(path);
		Native_startPlay();
		needDecoding = true;
		return true;
	}

	public boolean stopPlay(){
		return Native_stopPlay() == 0 ? true : false;
	}
	
	public boolean cleanup(){
		Native_cleanFfmpeg();
		return true;
	}
	
	public void run(){
		
		while(!isExit){
			
			if(!needDecoding){
				try{
					Thread.sleep(100);
				}catch(Exception e){
					e.printStackTrace();
				}
			}
					
			Native_decodeOneFrame();
			
			Native_updateSurface(mPlayerView);
		}
	}
	private native int Native_initFfmpeg();
	private native int Native_setupFile(String path);
	private native int Native_startPlay();
	private native int Native_cleanFfmpeg();
	private native int Native_stopPlay();
	private native int Native_updateSurface(PlayerView view);
	private native int Native_decodeOneFrame();
}